/*******************************************************************************
 * Copyright (c) 2014 Steffen Mikkelsen
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Steffen Mikkelsen - initial API and implementation
 ******************************************************************************/
package com.subsecret.secureconnection.client;

import java.io.IOException;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

public class Client {
    
    private static String optionDisableCompression = "disable-compression";
    private static String optionDisableEncryption = "disable-encryption";
    private static String optionDisableObfuscation = "disable-obfuscation";
    private static String optionForwardMapping = "L";
    private static String optionReverseMapping = "R";    
    private static String optionPassword = "password";
    
    public static void main(String args[]) throws Exception {
        OptionParser optionParser = createOptionParser();
        if (args.length == 0) {
            printHelpAndExit(null, optionParser);
        }
        String password = null;
        OptionSet options = optionParser.parse(args);
        if (!options.has(optionPassword)) {
            // if no password supplied - get it from the commandline (like ssh)
            System.out.print("Password: ");
            password = new String(System.console().readPassword());
            optionParser.printHelpOn(System.out);         
        }
        else {
            password = (String) options.valueOf(optionPassword);
        }
         System.out.println(password);   
        //List<?> nonOptionArguments = options.nonOptionArguments();
        
        ClientConnInfo info = parseArguments(options);
        
        establishConnection(info);
    }
    
    private static void printHelpAndExit(String cause, OptionParser optionParser) throws IOException {
        
        System.out.println("Usage: OPTIONS host[:port]");
        System.out.println();
        optionParser.printHelpOn(System.out);
        if (cause != null) {
            System.out.println();
            System.out.println(cause);
        }
        System.exit(1);
    }
    
    private static OptionParser createOptionParser() {
        OptionParser parser = new OptionParser();
        
        parser.accepts(optionPassword, "Server Password").withRequiredArg().ofType(String.class);
        
        parser.accepts(optionForwardMapping, "Forward port mapping (ie: localhost:5900:localhost:5900").withRequiredArg();
        parser.accepts(optionReverseMapping, "Reverse port mapping (ie: localhost:5900:localhost:5900").withRequiredArg();
        parser.accepts(optionDisableCompression, "Disable gzip compression");
        parser.accepts(optionDisableEncryption, "Disable encryption");
        parser.accepts(optionDisableObfuscation, "Disable obfuscation");
        return parser;
    }
    
    private static ClientConnInfo parseArguments(OptionSet options) {
        ClientConnInfo info = new ClientConnInfo();
        info.useCompression = !options.has(optionDisableCompression);
        info.useObfuscation = !options.has(optionDisableObfuscation);
        info.useEncryption = !options.has(optionDisableEncryption);
        
        return info;
    }
    
    private static void establishConnection(ClientConnInfo info) {
        
    }
    
}
